﻿using DABLL;
using DADAL;
using Newtonsoft.Json;
using Sunny.UI;
using WinFormium;
using WinFormium.Forms;

namespace DataAcquisition
{
    public class IndexFormium : Formium
    {

        #region 属性

        /// <summary>
        /// 服务
        /// </summary>
        private DABLL.Index IndexService = new DABLL.Index();

        /// <summary>
        /// 试压记录
        /// </summary>
        private TestRecordModel TestRecordModelService = new TestRecordModel();

        /// <summary>
        /// 播报
        /// </summary>
        private BroadcastServices Broad = new BroadcastServices();

        /// <summary>
        /// 数据连接状态
        /// </summary>
        private bool DataBaseState = false;

        /// <summary>
        /// 开始采集状态
        /// </summary>
        private bool StartState = false;

        /// <summary>
        /// 工程编号
        /// </summary>
        public string ProjectNumber = string.Empty;

        /// <summary>
        /// 工程
        /// </summary>
        private ProjectModel ProjectModelService = new ProjectModel();

        /// <summary>
        /// 试压模板
        /// </summary>
        private TemplateModel TemplateModelService = new TemplateModel();

        /// <summary>
        /// 当前阶段
        /// </summary>
        private string CurrentStage = string.Empty;

        /// <summary>
        /// 阶段List
        /// </summary>
        private List<string> StageList = new List<string>();

        /// <summary>
        /// 结果数组
        /// </summary>
        List<string> ResultList = new List<string>();

        /// <summary>
        /// 最终结果
        /// </summary>
        bool FinalResult = false;

        /// <summary>
        /// 阶段数量
        /// </summary>
        private int StageCount = 0;

        /// <summary> 起步阶段压力
        /// 
        /// </summary>
        private double StartStagePressure;

        /// <summary> 起步阶段压力上限
        /// 
        /// </summary>
        private double StartStagePressureMax = 0;

        /// <summary> 起步阶段压力下限
        /// 
        /// </summary>
        private double StartStagePressureMin = 0;

        /// <summary> 保持阶段压力
        /// 
        /// </summary>
        double HoldingStagePressure;

        /// <summary> 保持阶段压力上限
        /// 
        /// </summary>
        double HoldingStagePressureMax = 0;

        /// <summary> 保持阶段压力下限
        /// 
        /// </summary>
        double HoldingStagePressureMin = 0;

        /// <summary>
        /// 允许保持阶段
        /// </summary>
        bool EnableHoldingStage = false;

        /// <summary>
        /// 总时间
        /// </summary>
        private long TotalTime = 0;

        /// <summary>
        /// 当前阶段试压结果
        /// </summary>
        bool StageResult = false;

        /// <summary>
        /// 当前阶段总时间
        /// </summary>
        private long StageTotalTime = 0;

        /// <summary>
        /// 当前阶段剩余时间
        /// </summary>
        private string SurplusTotalTimeString = string.Empty;

        /// <summary>
        /// 开始起步稳压
        /// </summary>
        private bool StartStageHolding = false;

        /// <summary>起步阶段总时间
        /// 单位s
        /// </summary>
        private int StartStageTimeInterval = 20;

        /// <summary>当前步阶段剩余时间
        /// 单位s
        /// </summary>
        private int StartStageTime = 20;

        /// <summary>
        /// 采集点列表
        /// </summary>
        private List<double> PointList = new List<double>();

        /// <summary> 保持阶段错误次数
        /// 
        /// </summary>
        int HoldStageErrorTimes = 0;

        #region 计时器

        /// <summary>近端计时器
        /// 
        /// </summary>
        private System.Timers.Timer LocalCollectTimer = new System.Timers.Timer();

        /// <summary>远端计时器
        /// 
        /// </summary>
        private System.Timers.Timer RemoteCollectTimer = new System.Timers.Timer();

        /// <summary> 当前阶段计时器
        /// 
        /// </summary>
        private System.Timers.Timer StageTimer;

        /// <summary> 起步计时器
        /// 
        /// </summary>
        private System.Timers.Timer StartStageTimer;

        /// <summary> 保持阶段计时器
        /// 
        /// </summary>
        private System.Timers.Timer HoldingStageTimer;

        #endregion

        #region 近端数据

        /// <summary> 近端压力错误
        /// 
        /// </summary>
        private bool MainPressureError;

        /// <summary> 近端温度错误
        /// 
        /// </summary>
        private bool MainTemplatureError;

        /// <summary>主风扇
        /// 
        /// </summary>
        private bool MainFanState;

        /// <summary>主压力
        /// 
        /// </summary>
        private double MainPressure = 0;

        /// <summary>主机箱温度
        /// 
        /// </summary>
        private double MainMachineTemplature = 0;

        /// <summary>主环境温度
        /// 
        /// </summary>
        private double MainEnviromentTemplature = 0;

        /// <summary>主介质温度
        /// 
        /// </summary>
        private double MainMediumTemplature = 0;

        #endregion

        #region 远端数据

        /// <summary> 远端压力错误
        /// 
        /// </summary>
        private bool RemotePressureError;

        /// <summary> 远端温度错误
        /// 
        /// </summary>
        private bool RemoteTemplatureError;


        /// <summary>远端风扇
        /// 
        /// </summary>
        private bool RemoteFanState;

        /// <summary>远端压力
        /// 
        /// </summary>
        private double RemotePressure = 0;

        /// <summary>远端机温度
        /// 
        /// </summary>
        private double RemoteMachineTemplature = 0;

        /// <summary>远端介质温度
        /// 
        /// </summary>
        private double RemoteMediumTemplature = 0;

        #endregion

        #endregion

        #region 自定义方法

        #region 消息
        /// <summary> 前端Loading消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void LayerLoading(string msg)
        {
            string js = string.Format("layerLoading('{0}')", msg);
            EvaluateJavaScript(js);
        }

        /// <summary> 关闭前端Loading消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void LayerLoadingClose()
        {
            string js = string.Format("endLayerLoading()");
            EvaluateJavaScript(js);
        }

        /// <summary> 前端Error消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void LayerError(string msg)
        {
            LayerMsg(msg, 2);
        }

        /// <summary> 前端Success消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        private void LayerSuccess(string msg)
        {
            LayerMsg(msg, 1);
        }

        /// <summary>前端消息
        /// 
        /// </summary>
        /// <param name="msg"></param>
        /// <param name="icon"></param>
        private void LayerMsg(string msg, int icon)
        {
            string js = string.Format("layerMessage('{0}','{1}')", msg, icon);
            EvaluateJavaScript(js);
        }
        #endregion

        /// <summary>
        /// 执行js
        /// </summary>
        /// <param name="js"></param>
        private void EvaluateJavaScript(string js)
        {
            if (js is not null)
            {
                try
                {
                    EvaluateJavaScriptAsync(js);
                }
                catch (Exception)
                {
                }
            }
        }

        /// <summary>
        /// 设置服务
        /// </summary>
        /// <param name="name"></param>
        private void SetupServices()
        {
            LayerLoading("加载设备...");
            Broad.Setup();
            IndexService.Setup();
            LayerLoadingClose();
        }

        /// <summary>
        /// 检查数据库状态
        /// </summary>
        private bool CheckDataBaseState()
        {
            if (!DataBaseState)
            {
                LayerError("数据库连接失败，无法操作");
            }
            return DataBaseState;
        }

        /// <summary>
        /// 检查试压工程状态
        /// </summary>
        private bool CheckProjectState()
        {
            if (ProjectModelService.Number.IsNullOrEmpty())
            {
                return false; 
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// 检查开始采集状态
        /// </summary>
        private bool CheckStartState()
        {
            if (StartState)
            {
                LayerError("已开始采集，无法操作");
            }
            return StartState;
        }

        /// <summary>
        /// 读取压力
        /// </summary>
        private double ReadPressure(string key, string completion)
        {
            Dictionary<string, object>? back = IndexService.ReadRegisters(key);

            if (back is not null)
            {
                string alarm = "0";
                string log = string.Empty;
                ushort[] shorts = (ushort[])back["data"];
                DevicePointModel? model = IndexService.DevicePointWithKey(key);
                if (model is not null)
                {
                    double outMax = model.OutMax;
                    double outMin = model.OutMin;
                    double rangeMax = model.RangeMax;
                    double rangeMin = model.RangeMin;
                    double pressure = (shorts[0] - outMin) / (outMax - outMin);
                    pressure = Math.Round(pressure * (rangeMax - rangeMin) + rangeMin, 3);
                    if (model.Amend > 0)
                    {
                        pressure = Math.Round(pressure * model.Amend, 3);
                    }
                    if (model.AlarmMax > 0 && pressure > model.AlarmMax)
                    {
                        alarm = "1";
                        log = string.Format("【{0}:】【{1}】超过限高阈值【{2}】", model.Name, pressure, model.AlarmMax);
                    }
                    else if (model.AlarmMin > 0 && pressure < model.AlarmMin)
                    {
                        alarm = "1";
                        log = string.Format("【{0}:】【{1}】低于最低阈值【{2}】", model.Name, pressure, model.AlarmMin);
                    }
                    else
                    {
                        alarm = "0";
                        log = string.Format("【{0}:】【{1}】", model.Name, pressure);
                    }
                    //Log.Debug(log);
                    if (completion.Length > 0)
                    {
                        string js = string.Format("{0}('{1}','{2}')", completion, pressure, alarm);
                        EvaluateJavaScript(js);
                    }
                    return pressure;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                //Log.Error(string.Format("【{0}】读取失败", key));
                return -1;
            }
        }

        /// <summary>
        /// 读取温度
        /// </summary>
        private double ReadTemplature(string key, string completion)
        {
            Dictionary<string, object>? back = IndexService.ReadRegisters(key);

            if (back is not null)
            {
                string alarm = "0";
                string log = string.Empty;
                ushort[] shorts = (ushort[])back["data"];
                DevicePointModel? model = IndexService.DevicePointWithKey(key);
                if (model is not null)
                {
                    float temperature = (Tool.ConvertUshort(shorts[0]) / 10.0f);
                    double backTemperature = Math.Round(temperature, 3);
                    if (model.Amend > 0)
                    {
                        backTemperature = Math.Round(backTemperature * model.Amend, 3);
                    }
                    if (model.AlarmMax > 0 && backTemperature > model.AlarmMax)
                    {
                        alarm = "1";
                        log = string.Format("【{0}:】【{1}】超过限高阈值【{2}】", model.Name, backTemperature, model.AlarmMax);
                    }
                    else if (model.AlarmMin > 0 && backTemperature < model.AlarmMin)
                    {
                        alarm = "1";
                        log = string.Format("【{0}:】【{1}】低于最低阈值【{2}】", model.Name, backTemperature, model.AlarmMin);
                    }
                    else
                    {
                        alarm = "0";
                        log = string.Format("【{0}:】【{1}】", model.Name, backTemperature);
                    }
                    //Log.Debug(log);
                    return temperature;
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                //Log.Error(string.Format("【{0}】读取失败", key));
                return -1;
            }
        }

        /// <summary>
        /// 读取线圈
        /// </summary>
        private int ReadCoils(string key, string completion)
        {
            Dictionary<string, object>? back = IndexService.ReadRegisters(key);

            if (back is not null)
            {
                string log = string.Empty;
                bool[] shorts = (bool[])back["data"];
                DevicePointModel? model = IndexService.DevicePointWithKey(key);
                if (model is not null)
                {
                    bool res = shorts[0];
                    //Log.Debug(string.Format("【{0}:】【{1}】", model.Name, shorts[0]));
                    if (res)
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
                else
                {
                    return -1;
                }
            }
            else
            {
                //Log.Error(string.Format("【{0}】读取失败", key));
                return -1;
            }
        }

        /// <summary>刷新工程信息
        /// 
        /// </summary>
        public void ReloadProjectInfo()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = ProjectNumber;
            List<ProjectModel> list = SqlSugarHelper.Inquireable<ProjectModel>(where);
            if (list.Count == 0)
            {
                LayerError("试压工程信息不存在，请重新选择");
            }
            else
            {
                ProjectModelService = list[0];
                string data = string.Format("updateProjectInfo('{0}')", ProjectModelService.Name);
                EvaluateJavaScriptAsync(data);
                ReloadTemplateInfo();
            }
        }

        /// <summary> 
        /// 当前阶段计时
        /// </summary>
        private void CheckStage(object? sender, System.Timers.ElapsedEventArgs e)
        {
            if (StageTotalTime > 0)
            {
                StageTotalTime--;
                SurplusTotalTimeString = Tool.SecondToHMS(StageTotalTime);
                UpdateCurrentStageInfo();
            }
            else if (StageTotalTime == 0)
            {
                EnableHoldingStage = false;
                PointList.Clear();
                StageTimer.Stop();
                StartStageTimer.Stop();
                ReStartTimer();
                string state = "不合格";
                string stage = CurrentStage;
                if (StageResult == true)
                {
                    state = "合格";
                    ResultList.Add("true");
                }
                else
                {
                    ResultList.Add("false");
                }
                string msg = string.Format("{0}检测{1}，请注意安全", StageString(CurrentStage), state);
                Broad.Broadcast(msg);
                //Thread.Sleep(100);
                UpdateStageResult(state,stage);
                StageResult = false;
                if (StageList.Count > 1)
                {
                    StageList.RemoveAt(0);
                    UpdateStartStageInfo(StageList[0]);
                    StartStageTimer.Start();
                    StageTimer.Start();
                }
                else if(StageList.Count == 1)
                {
                    StageList.RemoveAt(0);
                    string res = "不合格";
                    if (!ResultList.Contains("false"))
                    {
                        res = "合格";
                        FinalResult = true;
                    }
                    string js = string.Format("updateCurrentStage('{0}','{1}')", res, "试压结果");
                    EvaluateJavaScript(js);
                    StopCollect();
                }
                else
                {
                    StopCollect();
                    StageTimer.Stop();
                    StartStageTimer.Stop();
                }
            }

            if (EnableHoldingStage)
            {
                if (MainPressure > HoldingStagePressureMax | MainPressure < HoldingStagePressureMin)
                {
                    Log.Error(string.Format("【{0}】,压力异常：【{1}】【{2}-{3}】", StageString(CurrentStage), MainPressure, HoldingStagePressureMin, HoldingStagePressureMax));
                    if (HoldStageErrorTimes <= 3)
                    {
                        HoldStageErrorTimes++;
                    }
                    else
                    {
                        StageResult = false;
                        EnableHoldingStage = false;
                        ReStartTimer();
                        StartStageTimer.Start();
                    }
                }
                else
                {
                    StageResult = true;
                    Log.Error(string.Format("【{0}】：【{1}】保持剩余：【{2}】", StageString(CurrentStage), MainPressure, SurplusTotalTimeString));
                }
            }
        }

        /// <summary> 
        /// 起步阶段
        /// </summary>
        private void CheckStartStage(object? sender, System.Timers.ElapsedEventArgs e)
        {
            string stageString = StageString(StageList[0]);
            string log = string.Empty;
            if ((MainPressure > StartStagePressureMax))
            {
                ReStartTimer();
                log = string.Format("当前压力【{0}】>【{1}】的标准", MainPressure, StartStagePressureMax);
            }
            else if ((MainPressure < StartStagePressureMin))
            {
                ReStartTimer();
                log = string.Format("当前压力【{0}】<【{1}】的标准", MainPressure, StartStagePressureMin);
            }
            else if ((MainPressure >= StartStagePressureMin) && (!StartStageHolding) && MainPressure > 0)
            {
                log = string.Format("【{0}】开始稳压【{1}】，范围：【{2}-{3}】", stageString, StartStagePressure, StartStagePressureMin, StartStagePressureMax);
                StartStageHolding = true;
            }
            if (log.Length > 0)
            {
                Log.Debug(log);
            }
            if (StartStageHolding)
            {
                if (StartStageTime == 0)
                {
                    if (PointList.Count == 0)
                    {
                        log = string.Format("【{0}】稳压通过。", stageString);
                        Log.Debug(log);
                    }
                    if (PointList.Count < Config.PointCount)
                    {
                        PointList.Add(MainPressure);
                        log = string.Format("【{0}】采集点位数量：【{1}】", stageString, PointList.Count);
                        Log.Debug(log);
                    }
                    else
                    {
                        double sum = PointList.Sum();
                        PointList.Clear();
                        if (StageList.Count >= 1)
                        {
                            StartStageTimer.Stop();
                            HoldingStagePressure = Math.Round(sum / Convert.ToSingle(Config.PointCount), 3);
                            UpdateHoldingStageInfo(CurrentStage);
                            EnableHoldingStage = true;
                        }
                    }
                }
                else
                {
                    log = string.Format("【{0}】稳压剩余【{1}】s", stageString, StartStageTime);
                    Log.Debug(log);
                    if ((MainPressure >= StartStagePressureMin) && (MainPressure <= StartStagePressureMax))
                    {
                        StartStageTime--;
                    }
                }
            }
        }

        /// <summary>
        /// 更新当前阶段信息
        /// </summary>
        private void UpdateCurrentStageInfo()
        {
            string stageString = StageString(CurrentStage);
            string js = string.Format("updateCurrentStage('{0}','{1}')", SurplusTotalTimeString, stageString);
            EvaluateJavaScript(js);
        }

        /// <summary> 更新保持阶段信息
        /// 
        /// </summary>
        /// <param name="stage"> 阶段</param>
        private void UpdateHoldingStageInfo(string stage)
        {
            if (TemplateModelService is not null)
            {
                string stage_estimate_way = string.Empty;
                double stage_estime_value = 0;
                if (stage == "first")
                {
                    stage_estimate_way = TemplateModelService.FirstStageEstimateWay;
                    stage_estime_value = TemplateModelService.FirstStageEstimateValue;
                }
                else if (stage == "second")
                {
                    stage_estimate_way = TemplateModelService.SecondStageEstimateWay;
                    stage_estime_value = TemplateModelService.SecondStageEstimateValue;
                }
                else if (stage == "third")
                {
                    stage_estimate_way = TemplateModelService.ThirdStageEstimateWay;
                    stage_estime_value = TemplateModelService.ThirdStageEstimateValue;
                }
                else if (stage == "intensity")
                {
                    stage_estimate_way = TemplateModelService.IntensityStageEstimateWay;
                    stage_estime_value = TemplateModelService.IntensityStageEstimateValue;
                }
                else if (stage == "rigor")
                {
                    stage_estimate_way = TemplateModelService.RigorStageEstimateWay;
                    stage_estime_value = TemplateModelService.RigorStageEstimateValue;
                }

                if (stage_estimate_way == "固定数值")
                {
                    HoldingStagePressureMin = HoldingStagePressure - stage_estime_value;
                    HoldingStagePressureMax = HoldingStagePressure + stage_estime_value;
                }
                else
                {
                    HoldingStagePressureMax = Math.Round(HoldingStagePressure * (1 + (stage_estime_value / 100.0f)), 3);
                    HoldingStagePressureMin = Math.Round(HoldingStagePressure * (1 - (stage_estime_value / 100.0f)), 3);
                }
                string log = string.Format("【{0}】,保持基准：【{1}】【{2}-{3}】", StageString(CurrentStage), HoldingStagePressure, HoldingStagePressureMin, HoldingStagePressureMax);
                Log.Debug(log);
            }
        }

        /// <summary>
        /// 更新当前阶段的试压状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="res"></param>
        private void UpdateStageResult(string res,string stage)
        {
            string id = string.Empty;
            if (stage == "first")
            {
                id = "firstStage";
            }
            else if (stage == "second")
            {
                id = "secondStage";
            }
            else if (stage == "third")
            {
                id = "thirdStage";
            }
            else if (stage == "intensity")
            {
                id = "intensityStage";
            }
            else if (stage == "rigor")
            {
                id = "rigorStage";
            }
            string js = string.Format("updateStage('#{0}','{1}')", id, res);
            EvaluateJavaScript(js);

        }

        /// <summary>重置起步计时器
        /// 
        /// </summary>
        private void ReStartTimer()
        {
            StartStageHolding = false;
            StartStageTime = StartStageTimeInterval;
        }

        /// <summary>
        /// 读取近端数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadMainRegister(object? sender, System.Timers.ElapsedEventArgs e)
        {
            UpdateTotalTime();
            Task.Run(() =>
            {
                int sleep = 100;
                int fan = ReadCoils("LFan", "");
                if (fan != -1)
                {
                    if (fan == 1)
                    {
                        MainFanState = true;
                    }
                    else
                    {
                        MainFanState = false;
                    }
                }

                Thread.Sleep(sleep);
                double mpressure = ReadPressure("LPressure", "updateMainPressure");
                if (mpressure != -1)
                {
                    MainPressure = mpressure;
                    MainPressureError = false;
                }
                else
                {
                    MainPressureError = true;
                }

                Thread.Sleep(sleep);
                bool enviromentTemplatureResult = false;
                double enviromentTemplature = ReadTemplature("LEnviromentTemperature", "0");
                if (enviromentTemplature != -1)
                {
                    MainEnviromentTemplature = Math.Round(enviromentTemplature, 3);
                    enviromentTemplatureResult = true;
                }

                Thread.Sleep(sleep);
                bool mediumTemplatureResult = false;
                double mediumTemplature = ReadTemplature("LMediumTemperature", "0");
                if (mediumTemplature != -1)
                {
                    MainMediumTemplature = Math.Round(mediumTemplature, 3);
                    mediumTemplatureResult = true;
                }

                Thread.Sleep(sleep);
                double machineTemplature = ReadTemplature("LMachineTemplature", "");
                if (machineTemplature != -1)
                {
                    MainMachineTemplature = Math.Round(machineTemplature, 3);
                }

                MainTemplatureError = !(enviromentTemplatureResult && mediumTemplatureResult);
                Dictionary<string, string> info = new Dictionary<string, string>();
                info["fan"] = fan.ToString();
                info["machineTemplature"] = MainMachineTemplature.ToString();
                info["enviromentTemplature"] = MainEnviromentTemplature.ToString();
                info["mediumTemplature"] = MainMediumTemplature.ToString();
                info["pressure"] = MainPressure.ToString();
                string js = string.Format("updateMainInfo('{0}')", JsonConvert.SerializeObject(info));
                EvaluateJavaScript(js);
            });
        }
        /// <summary>
        /// 读取远端数据
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadRemoteRegister(object? sender, System.Timers.ElapsedEventArgs e)
        {
            int sleep = 70;

            int fan = ReadCoils("RFan", "");
            if (fan != -1)
            {
                if (fan == 1)
                {
                    RemoteFanState = true;
                }
                else
                {
                    RemoteFanState = false;
                }
            }

            Thread.Sleep(sleep);
            double mediumTemplature = ReadTemplature("RMediumTemperature", "");
            if (mediumTemplature != -1)
            {
                RemoteMediumTemplature = Math.Round(mediumTemplature, 3);
            }

            Thread.Sleep(sleep);
            double machineTemplature = ReadTemplature("RMachineTemplature", "");
            if (machineTemplature != -1)
            {
                RemoteTemplatureError = false;
                RemoteMachineTemplature = Math.Round(machineTemplature, 3);
            }
            else
            {
                RemoteTemplatureError = true;
            }

            Thread.Sleep(sleep);
            double mpressure = ReadPressure("RPressure", "updateRemotePressure");
            if (mpressure != -1)
            {
                RemotePressureError = false;
                RemotePressure = mpressure;
            }
            else
            {
                RemotePressureError = true;
            }

            Dictionary<string, string> info = new Dictionary<string, string>();
            info["fan"] = "false";
            info["machineTemplature"] = RemoteMachineTemplature.ToString();
            info["mediumTemplature"] = RemoteMediumTemplature.ToString();
            info["pressure"] = mpressure.ToString();
            string js = string.Format("updateRemoteInfo('{0}')", JsonConvert.SerializeObject(info));
            EvaluateJavaScript(js);
        }

        /// <summary>
        /// 计时器
        /// </summary>
        /// <returns></returns>
        private System.Timers.Timer CollectTimer()
        {
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 1000;
            timer.AutoReset = true;//是否重复执行
            return timer;
        }

        private void UpdateTotalTime() 
        {
            TotalTime++;
            string time = Tool.SecondToHMS(TotalTime);
            try
            {
                string data = string.Format("updateTotalTime('{0}')", time);
                EvaluateJavaScriptAsync(data);
            }
            catch (Exception)
            {
            }
        }

        /// <summary>刷新模板信息
        /// 
        /// </summary>
        public void ReloadTemplateInfo()
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = ProjectModelService.TemplateID;
            List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(where);

            if (list.Count <= 0)
            {
                LayerError("试压模板错误，请重新选择试压工程");
                return;
            }
            else
            {
                TemplateModelService = list[0];
            }
            string state = "----";
            string select_stage = state;
            string hidden_stage = "";
            if (StageList.Count > 0)
            {
                StageList.Clear();
            }
            if (TemplateModelService.FirstStagePressure > 0)
            {
                StageList.Add("first");
            }
            else
            {
                state = hidden_stage;
            }
            UpdateCurrentStageResult("firstStage", state);

            if (TemplateModelService.SecondStagePressure > 0)
            {
                StageList.Add("second");
                state = select_stage;
            }
            else
            {
                state = hidden_stage;
            }
            UpdateCurrentStageResult("secondStage", state);

            if (TemplateModelService.ThirdStagePressure > 0)
            {
                StageList.Add("third");
                state = select_stage;
            }
            else
            {
                state = hidden_stage;
            }
            UpdateCurrentStageResult("thirdStage", state);

            if (TemplateModelService.IntensityStagePressure > 0)
            {
                StageList.Add("intensity");
                state = select_stage;
            }
            else
            {
                state = hidden_stage;
            }
            UpdateCurrentStageResult("intensityStage", state);

            if (TemplateModelService.RigorStagePressure > 0)
            {
                StageList.Add("rigor");
                state = select_stage;
            }
            else
            {
                state = hidden_stage;
            }
            UpdateCurrentStageResult("rigorStage", state);
            StageCount = StageList.Count;

        }

        /// <summary>
        /// 更新当前阶段信息
        /// </summary>
        /// <param name="stage"></param>
        private void UpdateStartStageInfo(string stage)
        {
            CurrentStage = stage;

            if (TemplateModelService is not null)
            {
                string stageString = StageString(stage);
                double multiple = 1;
                int tmp = 1;
                int time = 0;
                string unit = string.Empty;
                if (stage == "first")
                {
                    StartStagePressure = TemplateModelService.FirstStagePressure;
                    unit = TemplateModelService.FirstStageTimeUnit;
                    if (TemplateModelService.FirstStageTimeUnit == "小时")
                    {
                        multiple = tmp;
                    }
                    time = Convert.ToInt16(TemplateModelService.FirstStageTime);
                }
                else if (stage == "second")
                {
                    StartStagePressure = TemplateModelService.SecondStagePressure;
                    unit = TemplateModelService.SecondStageTimeUnit;
                    if (TemplateModelService.SecondStageTimeUnit == "小时")
                    {
                        multiple = tmp;
                    }
                    time = Convert.ToInt16(TemplateModelService.SecondStageTime);
                }
                else if (stage == "third")
                {
                    StartStagePressure = TemplateModelService.ThirdStagePressure;
                    unit = TemplateModelService.ThirdStageTimeUnit;
                    if (TemplateModelService.ThirdStageTimeUnit == "小时")
                    {
                        multiple = tmp;
                    }
                    time = Convert.ToInt16(TemplateModelService.ThirdStageTime);
                }
                else if (stage == "intensity")
                {
                    StartStagePressure = TemplateModelService.IntensityStagePressure;
                    unit = TemplateModelService.IntensityStageTimeUnit;
                    if (TemplateModelService.IntensityStageTimeUnit == "小时")
                    {
                        multiple = tmp;
                    }
                    time = Convert.ToInt16(TemplateModelService.IntensityStageTime);
                }
                else if (stage == "rigor")
                {
                    StartStagePressure = TemplateModelService.RigorStagePressure;
                    unit = TemplateModelService.RigorStageTimeUnit;
                    if (TemplateModelService.RigorStageTimeUnit == "小时")
                    {
                        multiple = tmp;
                    }
                    time = Convert.ToInt16(TemplateModelService.RigorStageTime);
                }

                StartStagePressureMax = Math.Round(StartStagePressure * (1 + (TemplateModelService.ErrorRange / 100.0f)), 3);
                StartStagePressureMin = Math.Round(StartStagePressure * (1 - (TemplateModelService.ErrorRange / 100.0f)), 3);
                StageTotalTime = (long)(time * tmp * multiple);
                string msg = string.Format("{0}{1}{2}开始倒计时", stageString, time, unit);
                if (Config.StageBroad)
                {
                    Broad.Broadcast(msg);
                }
                Log.Debug(msg);
                string log = string.Format("【{0}】,起步基准：【{1}】【{2}-{3}】", StageString(CurrentStage), StartStagePressure, StartStagePressureMin, StartStagePressureMax);
                Log.Debug(log);
            }
        }

        /// <summary>
        /// 转换当前阶段
        /// </summary>
        /// <param name="stage"></param>
        /// <returns></returns>
        private string StageString(string stage)
        {
            string stageString = "";
            if (stage == "first")
            {
                stageString = "第一阶段";
            }
            else if (stage == "second")
            {
                stageString = "第二阶段";
            }
            else if (stage == "third")
            {
                stageString = "第三阶段";
            }
            else if (stage == "intensity")
            {
                stageString = "强度阶段";
            }
            else if (stage == "rigor")
            {
                stageString = "严密性阶段";
            }
            return stageString;
        }

        /// <summary>
        /// 更新当前阶段的试压状态
        /// </summary>
        /// <param name="id"></param>
        /// <param name="res"></param>
        private void UpdateCurrentStageResult(string id, string res)
        {
            string js = string.Format("updateStage('#{0}','{1}')", id, res);
            EvaluateJavaScript(js);
        }

        /// <summary>
        /// 添加试压记录
        /// </summary>
        private int AddTestRecord()
        {
            TestRecordModelService.Number = Convert.ToInt32(DateTimeOffset.Now.ToUnixTimeSeconds());
            TestRecordModelService.StartTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (ProjectModelService is not null)
            {
                TestRecordModelService.ProjectNumber = ProjectModelService.Number;
            }
            List<TestRecordModel> list = new List<TestRecordModel>();
            list.Add(TestRecordModelService);
            int result = SqlSugarHelper.Insertable(list);
            return result;
        }

        /// <summary>
        /// 注册方法
        /// </summary>
        private void RegisterJavaScriptMessagHandler()
        {
            RegisterJavaScriptMessagHandler("ShowForm", args =>
            {
                bool res = CheckDataBaseState();
                if (!res)
                {
                    return;
                }

                res = CheckStartState();
                if (res)
                {
                    return;
                }

                string? data = args;
                if (data is not null)
                {
                    if (data == "DeviceManage")
                    {
                        InvokeOnUIThread(() =>
                        {
                            DeviceManageForm frm = new DeviceManageForm();
                            frm.Show(this);
                        });
                    }
                    else if (data == "Template")
                    {
                        InvokeOnUIThread(() =>
                        {
                            TemplateAddForm frm = new TemplateAddForm();
                            frm.Show(this);
                        });
                    }
                    else if (data == "Project")
                    {
                        InvokeOnUIThread(() =>
                        {
                            ProjectSelectForm frm = new ProjectSelectForm();
                            frm.OwnerFormium = this;
                            frm.Show(this);
                        });
                    }
                    else if (data == "Set")
                    {
                        InvokeOnUIThread(() =>
                        {
                            SetForm frm = new SetForm();
                            frm.Show(this);
                        });
                    }
                }
            });

            RegisterJavaScriptMessagHandler("FormState", args =>
            {
                string? data = args;
                if (data is not null)
                {
                    if (data == "minimize")
                    {
                        InvokeOnUIThread(() =>
                        {
                            WindowState = FormiumWindowState.Minimized;
                        });
                    }
                    else if (data == "close")
                    {
                        InvokeOnUIThread(() =>
                        {
                            this.Close();
                        });
                    }
                }
            });

            RegisterJavaScriptMessagHandler("Collect", args =>
            {
                bool res = CheckDataBaseState();
                if (!res)
                {
                    return;
                }
                string? data = args;
                if (data is not null)
                {
                    if (data == "start")
                    {
                        res = CheckProjectState();
                        if (!res)
                        {
                            LayerError("请选择试压工程");
                            return;
                        }

                        res = CheckStartState();
                        if (res)
                        {
                            return;
                        }

                        if (Config.StartBroad)
                        {
                            Broad.Broadcast(Config.StartBroadMessage);
                            Thread.Sleep(100);
                        }
                        StartCollect();
                    }
                    else if (data == "stop")
                    {
                        StopCollect();
                    }
                }
            });

            RegisterJavaScriptMessagHandler("Report", args =>
            {
                string? data = args;
                if (data is not null)
                {
                    InvokeOnUIThread(() =>
                    {
                        ReportFormium frm = new ReportFormium();
                        frm.TemplateModelServices = TemplateModelService;
                        frm.ProjectItemServices = ProjectModelService;
                        frm.TestRecordModels = TestRecordModelService;
                        frm.ImageData = data;
                        frm.ShowDialog(this);
                    });
                }
            });
        }

        /// <summary>
        /// 开始采集
        /// </summary>
        private void StartCollect()
        {
            int result = AddTestRecord();
            if (result > 0)
            {
                ReloadProjectInfo();
                UpdateStartStageInfo(StageList[0]);
                LocalCollectTimer.Start();
                RemoteCollectTimer.Start();
                StageTimer.Start();
                StartStageTimer.Start();
                StartState = true;
                string data = string.Format("startCollect()");
                EvaluateJavaScript(data);
            }
            else
            {
                LayerError("请重新开始");
            }
        }

        /// <summary>
        /// 更新试压记录
        /// </summary>
        private int UpdateTestRecord()
        {
            TestRecordModelService.EndTime = DateTime.Parse(TestRecordModelService.StartTime).AddSeconds(TotalTime).ToString("yyyy-MM-dd HH:mm:ss");
            TestRecordModelService.TotalTime = TotalTime.ToString();
            if (ResultList.Count < StageCount)
            {
                FinalResult = false;
                TestRecordModelService.Result = 0;
            }
            else
            {
                bool res = ResultList.Contains("false");
                TestRecordModelService.Result = !res ? 1 : 0;
            }
            List<TestRecordModel> list = new List<TestRecordModel>();
            list.Add(TestRecordModelService);
            int result = SqlSugarHelper.Updateable<TestRecordModel>(list);
            return result;
        }

        /// <summary>
        /// 停止采集
        /// </summary>
        private void StopCollect()
        {
            UpdateTestRecord();
            FinalResult = false;
            LocalCollectTimer.Stop();
            RemoteCollectTimer.Stop();
            StageTimer.Stop();
            ReStartTimer();
            StartStageTimer.Stop();
            StartState = false;
            StartStagePressure = 0;
            StartStagePressureMin = 0;
            StartStagePressureMax = 0;
            StageTotalTime = 0;
            PointList.Clear();
            ResultList.Clear();
            string data = string.Format("stopCollect()");
            EvaluateJavaScript(data);
        }
        #endregion

        #region 系统方法
        public IndexFormium()
        {
            ///不显示窗体过度动画
            EnableSplashScreen = false;
            Icon icon = new Icon("logo.ico");
            Icon = icon;
            Sizable = false;//禁止调整窗口大小
            Url = "http://dataqcquisition.app.local/Index.html";
            PageLoadEnd += IndexFormium_PageLoadEnd;
            RegisterJavaScriptMessagHandler();

            LocalCollectTimer = CollectTimer();
            LocalCollectTimer.Elapsed += ReadMainRegister;

            RemoteCollectTimer = CollectTimer();
            RemoteCollectTimer.Elapsed += ReadRemoteRegister;

            StageTimer = CollectTimer();
            StageTimer.Elapsed += CheckStage;

            StartStageTimer = CollectTimer();
            StartStageTimer.Elapsed += CheckStartStage;
        }
        protected override FormStyle ConfigureWindowStyle(WindowStyleBuilder builder)
        {
            var style = builder.UseSystemForm();
            style.TitleBar = false;
            style.DefaultAppTitle = "数据采集";
            style.Maximizable = true;
            style.WindowState = FormiumWindowState.Maximized;
            style.StartCentered = StartCenteredMode.CenterScreen;
            style.ColorMode = FormiumColorMode.Light;

            if (Screen.PrimaryScreen is not null)
            {
                style.MinimumSize = Screen.PrimaryScreen.WorkingArea.Size;
            }
            return style;
        }

        /// <summary>页面加载完成
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void IndexFormium_PageLoadEnd(object? sender, PageLoadEndEventArgs e)
        {
            Task.Run(() => {
                int res = 0;
                if (SqlSugarHelper.State() == false)
                {
                    LayerError("连接数据库失败");
                    DataBaseState = false;
                }
                else
                {
                    res = 1;
                    LayerLoadingClose();
                    DataBaseState = true;
                    SetupServices();
                }
                string data = string.Format("setDataBaseState('{0}')", res);
                EvaluateJavaScript(data);
            });
        }
        #endregion
    }
}
